'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 购买挑战次数
 * @author agl
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var configUtil = require('../../../../util/configUtil');
var constantUtil = require('../../../../util/constantUtil');
var PlayerManager = require('../../../../libs/playerManager');
var taskService = require('../../../../services/taskService');
var roleService = require('../../../../services/roleService');
var expeditionService = require('../../../../services/expeditionService');
var soldierService = require('../../../../services/soldierService.js');
var dataApi = require('../../../../libs/dataApi.js');
var atkData = require('../../../../libs/war/atkData.js');
var vipConf = require('../../../../config/vipConf.js');
var expeditionConf = require('../../../../config/expeditionConf.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;

    var player_manager = new PlayerManager(role_id);
    var self_info = {
        hp_list: [],
        soldier_list: []
    };
    var rival_info = {
        hp_list: [],
        soldier_list: []
    };
    var expedition_model;
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['expedition', 'role', 'soldier', 'bag', 'tech', 'accrued'], cb);
            },
            function (cb) {
                expedition_model = player_manager.expedition;
                var role_model = player_manager.role;
                var monster_open_lvl = constantUtil.value('monster_open_lvl');
                if (role_model.lv < monster_open_lvl) {
                    return next(null, {
                        code: Code.USER_LEVEL_NOT_ENOUGH
                    });
                }

                // 初始化
                var old_remain_times = expedition_model.remain_times;
                if (expedition_model.rival_list.length === 0) {
                    expeditionService.reset(player_manager, function (err) {
                        if (!!err) {
                            return cb(err);
                        }
                        expedition_model.remain_times = old_remain_times;

                        cb();
                    });
                } else {
                    cb();
                }
            },
            function (cb) {
                var soldier_model = player_manager.soldier;
                self_info.soldier_list = expedition_model.soldier_list;

                var hp_list = self_info.hp_list;
                var soldier_list = self_info.soldier_list;
                self_info.soldier_power = soldierService.calcSoldierPower(soldier_model, soldier_list);

                var soldier_type_list = [];
                _.forEach(soldier_model.soldier_list, function (soldier_item) {
                    soldier_type_list.push(soldier_item.soldier_type);
                });

                var soldier_info_list = atkData.getSoldierInfoForType(soldier_type_list, player_manager);
                _.forEach(soldier_info_list, function (info) {
                    var curr_hp = expedition_model.getSoldierCurrHp(info.soldier_type, info.attr.hp);
                    hp_list.push({
                        soldier_type: info.soldier_type,
                        lv: info.lv,
                        star: info.star,
                        max_hp: info.attr.hp,
                        curr_hp: curr_hp
                    });
                });

                cb();
            },
            function (cb) {
                var expedition_model = player_manager.expedition;
                var next_pass_id = expeditionConf.getNextMonsterId(expedition_model.pass_id);
                var rival_idx = expeditionConf.getMonsterIndexById(next_pass_id);
                var def_info = expedition_model.rival_list[rival_idx];
                if (!!def_info) {
                    var hp_list = rival_info.hp_list;
                    var soldier_list = rival_info.soldier_list;
                    rival_info.soldier_power = def_info.soldier_power;
                    _.forEach(def_info.soldier_list, function (soldier_info) {
                        if (!!soldier_info) {
                            soldier_list.push(soldier_info.soldier_type);
                            hp_list.push({
                                soldier_type: soldier_info.soldier_type,
                                lv: soldier_info.lv,
                                star: soldier_info.star,
                                max_hp: soldier_info.attr.hp,
                                curr_hp: soldier_info.attr.curr_hp
                            });
                        } else {
                            soldier_list.push(0);
                            hp_list.push(null);
                        }
                    });
                }

                cb();
            },
            function (cb) {
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    self_info: self_info,
                    rival_info: rival_info
                }
            });
        }
    );
};
